<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        总点击数是：{{totalCount}}
        <button-counter v-on:count="onCount" v-for="num in 5"></button-counter>

        <!-- <button-counter v-on:count="totalCount++" v-for="num in 5"></button-counter> -->
        <!-- <button-counter v-on:count="totalCount++"></button-counter>
        <button-counter v-on:count="totalCount++"></button-counter>
        <button-counter v-on:count="totalCount++"></button-counter>
        <button-counter v-on:count="totalCount++"></button-counter>
        <button-counter v-on:count="totalCount++"></button-counter> -->
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script>
        Vue.component("ButtonCounter",{
            template:`<div>
                           <button @click="onClick">点击{{count}}</button>
                      </div>`,
            data(){
                return{
                    count:0,
                }
            },
            methods: {
                onClick(){
                    this.count++;
                    // 子组件改变父组件的数据是以自定义事件的形式
                    // 1.触发事件 emit("触发事件的名称")
                    // this.$emit("count");

                    // 2.触发事件且传递参数 emit("触发事件的名称",指定数据)
                    this.$emit("count",3)
                }
            }
        })

        var vm = new Vue({
            el: "#app",
            data:{
                totalCount: 0
            },
            methods: {
                // 38行传的参数3就会给到data,data就是3
                onCount(data){
                    console.log(data);
                    this.totalCount += data;

                }
            },
            components: {

            }
        })
    </script>
</body>
</html>